package com.example.android.ui.login;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.android.R;
import com.example.android.bean.Login;
import com.example.android.bean.LoginToken;
import com.example.android.ui.MainActivity;
import com.example.android.ui.base.BaseActivity;
import com.example.android.ui.register.RegisterActivity;

public class LoginActivity extends BaseActivity implements View.OnClickListener, LoginContract.ILoginView {
    private Button btnLogin;
    private TextView textRegister, textForget;
    private EditText username, password;
    private LoginPresenter loginPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        setTheme(R.style.Theme_Android);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initViews() {
        SharedPreferences sharedPreferences = getSharedPreferences("HBUserLogin", MODE_PRIVATE);
        Boolean isLogin = sharedPreferences.getBoolean("isLogin", false);
        Long expireTime = sharedPreferences.getLong("expireTime", 0);
        if (isLogin == true && System.currentTimeMillis() < expireTime) {
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }


        username = find(R.id.edtextLoginUserid);
        password = find(R.id.edtextLoginPasswd);
        btnLogin = find(R.id.btnLoginLogin);
        textRegister = find(R.id.textLoginRegister);
        textForget = find(R.id.textLoginForget);
        loginPresenter = new LoginPresenter(this);
        //点击事件
        btnLogin.setOnClickListener(this);
        textRegister.setOnClickListener(this);
        textForget.setOnClickListener(this);


    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnLoginLogin:
                loginPresenter.getLoginData(username.getText().toString(), password.getText().toString());
                break;
            case R.id.textLoginRegister:
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.textLoginForget:
                Toast.makeText(LoginActivity.this, "忘记密码开发中", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }


    @Override
    public void getLoginSuccess(Login login) {
        if (login.getCode() == 0) {
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            SharedPreferences sharedPreferences = getSharedPreferences("HBUserLogin", MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putBoolean("isLogin", true);
            editor.putString("jwt", login.getJwt());
            editor.putInt("userId", login.getUserInfo().getUserId());
            editor.putString("userAccount",login.getUserInfo().getUserAccount());
            editor.putString("userHeadimageurl",login.getUserInfo().getUserHeadimageUrl());
            editor.putString("userUsername",login.getUserInfo().getUserUsername());
            editor.putString("userBirthday",login.getUserInfo().getUserBirthDatetime());
            editor.commit();
            loginPresenter.getTokenData(login.getUserInfo().getUserId(), login.getJwt());
            startActivity(intent);
            finish();
        } else {
//            username.setText("");
            password.setText("");
            Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void getTokenSuccess(LoginToken loginToken) {
//        Log.i("LoginActivity","test");
        SharedPreferences sharedPreferences = getSharedPreferences("HBUserLogin", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putLong("expireTime", loginToken.getExpireTime().getTime());
        editor.commit();
    }

    @Override
    public void getLoginError(Throwable throwable) {
        Toast.makeText(LoginActivity.this, "服务器出错啦！！", Toast.LENGTH_SHORT).show();
    }
}
